<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\BrowsingHistory;
use app\common\model\Slide;
use app\common\model\SlideItem;
use app\common\model\SysMessage;
use app\common\model\SysMessageCategory;

class Publics extends Api{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function getBanner(){
        $position = request()->post('position','task');
        $slideId = Slide::getSlideIdByPosition($position);
        $this->success('请求成功',SlideItem::getListBySlideId($slideId));
    }

    public function noticeCategory(){
        $this->success('请求成功',SysMessageCategory::getList());
    }

    public function notice(){
        $categoryId = request()->post('categoryId',1);
        $this->success('请求成功',SysMessage::getList($categoryId,$this->auth->id));
    }

    public function noticeDetail(){
        $noticeId = request()->post('noticeId');
        if(empty($noticeId)){
            $this->error('参数错误');
        }

        SysMessage::where('id',$noticeId)->setInc('browse_num',1);
        $message = SysMessage::where('id',$noticeId)->field('id,category_id,title,content,from,image,image,browse_num,created_at')->find();
        if($this->auth->id && !empty($message)){
            $count = BrowsingHistory::where('message_id',$noticeId)->where('user_id',$this->auth->id)->count();
            if($count < 1){
                BrowsingHistory::create([
                    'message_id'=> $noticeId,
                    'user_id'   => $this->auth->id,
                ]);
            }
        }
        $this->success('请求成功',$message);
    }

    public function isShow()
    {
        $message = SysMessage::where('show','1')->field('title,content,from,created_at')->select();
        $this->success('请求成功',$message);
    }


    /**
     * 获取学历列表
     */
    public function getEducationList(){
        $this->success('请求成功',\app\common\model\User::$education);
    }

    /**
     * 获取关系列表
     */
    public function getRelationList(){
        $this->success('请求成功',\app\common\model\User::$userPosition);
    }

    /**
     * 获取地址列表
     */
    public function getAddressList()
    {
        $pid = $this->request->post('pid');
        $area = Area::where('pid',$pid)->select();
        $this->success('请求成功',$area);
    }
}